/******************************************************************************
 * @File name   :      PackageImpl.java
 *
 * @Author      :      JIZHANGS
 *
 * @Date        :      Jul 22, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2015 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * Jul 22, 2015 11:45:40 AM        JIZHANGS     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.mars.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cap.bts.crmdp.dealer.coupon.constants.CouponConstants;
import com.cap.bts.crmdp.dealer.coupon.dao.ITicketPriceOfDealerDao;
import com.cap.bts.crmdp.dealer.coupon.domain.TicketPriceOfDealer;
import com.cap.bts.crmdp.dealer.mars.service.IMarsPackageDefinitionService;
import com.cap.bts.crmdp.dealer.masterdata.dao.IMaintainDealerDao;
import com.cap.bts.crmdp.dealer.masterdata.domain.MasterDataDealerInfo;
import com.cap.bts.crmdp.dealer.meal.dao.IPackageEligibilityDao;
import com.cap.bts.crmdp.dealer.meal.domain.PackageDealerSelected;
import com.cap.bts.crmdp.dealer.meal.domain.PackageEligibilityModel;
import com.cap.bts.crmdp.dealer.meal.service.IMealManageService;
import com.cap.bts.crmdp.dealer.purchase.dao.IPackageDefinitionDao;
import com.cap.bts.crmdp.dealer.purchase.domain.PackageDefinition;
import com.cap.bts.crmdp.dealer.purchase.dto.PackageDefinitionResultsDto;
import com.cap.bts.framework.codemapping.dao.ICodeMappingDao;
import com.cap.bts.framework.codemapping.domain.CodeInfo;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.service.impl.BaseServiceImpl;

/**
 *
 */
@Transactional(rollbackFor = AppException.class)
@Service
public class MarsPackageDefinitionSerivceImpl extends BaseServiceImpl implements IMarsPackageDefinitionService{
   @Resource(name = "packageDefinitionDao")
   
   private IPackageDefinitionDao packageDefinitionDao;
   
   @Autowired
   IMaintainDealerDao dealerDao;
   @Autowired
   ICodeMappingDao codeMappingDao;
   @Autowired
   IPackageEligibilityDao packageEligibilityDao;
   @Autowired
   ITicketPriceOfDealerDao ticketPriceOfDealerDao;
   @Autowired
   IMealManageService mealManageService;
   
   public List<PackageDefinitionResultsDto> getAllPackageDefinitionList(Long dealerId) throws AppException{
       
       List<PackageDefinitionResultsDto> packageDefinitionResultsDtolist= new ArrayList<PackageDefinitionResultsDto>();
       
       List<PackageDefinition> list=packageDefinitionDao.getAllPackageDefinitionList();
       PackageDefinitionResultsDto dto=new PackageDefinitionResultsDto();
       //获取当前门店信息
       MasterDataDealerInfo dealerInfo = dealerDao.getDealerByDealerNum(String.valueOf(dealerId));
       //默认套餐为空设置
       dto.setId(Long.valueOf("-1"));
       dto.setName(null);
       dto.setPrice(null);
       dto.setIsPhysicalCard(0);
       packageDefinitionResultsDtolist.add(dto);
       for(int i=0;i<list.size();i++){
    	   //查看套餐的适用范围,适用范围外的要删掉
    	   
    	   //构造优先级的map
           Map<String,String> priority = new HashMap<String,String>();
           List<CodeInfo> priorityList = codeMappingDao.getCodeInfoByCodeType(CouponConstants.DEALERPROPERTY);
           if(priorityList != null && !priorityList.isEmpty()){
               for(int k=0; k<priorityList.size(); k++){
                   priority.put(priorityList.get(k).getCode().substring(2), priorityList.get(k).getCode());
               }
           }
           
           if(priority != null && !priority.isEmpty()){
        	   List<PackageEligibilityModel> packageEligibilityList = packageEligibilityDao.getPackageEligibilityByPackageDefId(list.get(i).getId());
        	   if(packageEligibilityList != null && !packageEligibilityList.isEmpty()){
        		   boolean isExist = false;
        		   for(int j=0; j<packageEligibilityList.size(); j++){
        			   if(priorityList != null && !priorityList.isEmpty()){
        	               for(int k=0; k<priorityList.size(); k++){
        	            	   if(priorityList.get(k).getCode().equals(packageEligibilityList.get(j).getDealerProperty())){
        	            		   String property = packageEligibilityList.get(j).getDealerProperty().substring(2);
                				   if(packageEligibilityList.get(j).getPropertyValue().equals(dealerDao.getDealersByProperty(property,dealerInfo.getDealerNum()))){
                					   isExist = true;
                				   }
                			   }
        	               }
        	           }
        		   }
        		   if(!isExist){
        			   list.remove(i);
					   i--;
					   continue;
        		   }
        	   }
           }
    	   
         //查找地方性价格
           Double price = null;
           List<TicketPriceOfDealer> ticketPriceOfDealerList = ticketPriceOfDealerDao.getTicketPriceOfDealerByDealerAndPackageDefId(dealerId,
               list.get(i).getId());
           if(ticketPriceOfDealerList != null && !ticketPriceOfDealerList.isEmpty()){
               price = ticketPriceOfDealerList.get(0).getPrice();
           }else{
               price = list.get(i).getPrice();
           }
           
           dto=new PackageDefinitionResultsDto();
           dto.setId(list.get(i).getId());
           dto.setName(list.get(i).getName());
           dto.setPrice(price);
           dto.setIsPhysicalCard(list.get(i).getIsPhysicalCard());
           dto.setIsPrepaymentCard(list.get(i).getIsPrepaymentCard());
           dto.setStatus(list.get(i).getStatus());
           dto.setCardDefinition(list.get(i).getCardDefinition());
           dto.setUnitOfPeriod(list.get(i).getUnitOfPeriod());
           dto.setValidPeriod(list.get(i).getValidPeriod());
           dto.setCardDefinition(list.get(i).getCardDefinition());
           packageDefinitionResultsDtolist.add(dto);
       }
       //去除门店不售卖的套餐
       for(int i=0; i<packageDefinitionResultsDtolist.size(); i++){
    	   PackageDefinitionResultsDto resultDto = packageDefinitionResultsDtolist.get(i);
    	   List<PackageDealerSelected> selectedList = mealManageService.getPackageSelected(String.valueOf(resultDto.getId()), String.valueOf(dealerId));
    	   if((selectedList == null || selectedList.isEmpty()) && resultDto.getId() != -1){
    		   packageDefinitionResultsDtolist.remove(resultDto);
    		   i--;
    	   }
       }
       return packageDefinitionResultsDtolist;
   }
}
